//
// Copyright (c) 2009 All Right Reserved
//
// vl
//
// 2018-12-01
// Contains ...
using LargoCommon.Interfaces;
using LargoCommon.Music;
using LargoPanels.Abstract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Media;
namespace LargoPanels.Editor
{
/// A base chart master.
public partial class EditorSpace : FrameworkElement
{
///
/// The corner cell
///
private CornerCell cornerCell;
#region Public properties - Cells
/// Gets or sets the bar cells.
/// The bar cells.
public List BarCells { get; set; }
/// Gets or sets the track cells.
/// The track cells.
public List LineCells { get; set; }
/// Gets or sets the editor tracks.
/// The editor tracks.
public List EditorLines { get; set; }
/// Gets or sets all cells.
/// all cells.
public List AllCells { get; set; }
/// Gets or sets the voice cells.
/// The voice cells.
public List VoiceCells { get; set; }
///
/// Gets the content cells.
///
///
/// The content cells.
///
public List ContentCells {
get {
var cells = new List();
foreach (var editorLine in this.EditorLines) {
cells.AddRange(editorLine.ContentCells);
}
return cells;
}
}
///
/// Gets the group cells.
///
///
/// The group cells.
///
public List GroupCells {
get {
var cells = new List();
foreach (var editorLine in this.EditorLines) {
cells.AddRange(editorLine.GroupCells);
}
return cells;
}
}
///
/// Gets the selected content cells.
///
///
/// The selected content cells.
///
public List SelectedContentCells {
get {
var contentCells = this.ContentCells;
var cells = (from cell in contentCells where cell.IsSelected select cell).ToList();
return cells;
}
}
///
/// Gets the selected group cells.
///
///
/// The selected group cells.
///
public List SelectedGroupCells {
get {
var groupCells = this.GroupCells;
var cells = (from cell in groupCells where cell.IsSelected select cell).ToList();
return cells;
}
}
///
/// Gets the selected content cells bar sorted.
///
///
/// The selected content cells bar sorted.
///
public List SelectedContentCellsBarSorted {
get {
var contentCells = this.ContentCells;
var cells = (from cell in contentCells where cell.IsSelected orderby cell.BarIndex select cell).ToList();
return cells;
}
}
///
/// Gets the selected elements.
///
///
/// The selected elements.
///
public List SelectedElements {
get {
var contentCells = this.ContentCells;
var elements = (from cell in contentCells where cell.IsSelected && cell.Element != null select cell.Element).ToList();
return elements;
}
}
///
/// Gets the selected bar cells.
///
///
/// The selected bar cells.
///
public List SelectedBarCells {
get {
var cells = (from cell in this.BarCells where cell.IsSelected select cell).ToList();
return cells;
}
}
///
/// Gets the selected line cells.
///
///
/// The selected line cells.
///
public List SelectedLineCells {
get {
var cells = (from cell in this.LineCells where cell.IsSelected select cell).ToList();
return cells;
}
}
#endregion
///
/// Shifts the line content right.
///
public void ShiftContentRight() {
var sortedCells = this.SelectedContentCellsBarSorted;
var lastCell = sortedCells.LastOrDefault();
if (lastCell == null) {
return;
}
MusicalElement rightElement;
var rightIndex = lastCell.BarIndex;
if (rightIndex < this.BarCells.Count) {
var rightBar = this.BarCells[rightIndex].Bar;
rightElement = rightBar.Elements[lastCell.Line.LineIndex];
}
else {
rightElement = lastCell.Element;
}
rightElement.Status = lastCell.Element.Status;
LineStatus lastStatus = null;
foreach (var cell in sortedCells) {
var s = cell.Element.Status;
if (lastStatus != null) {
cell.Element.Status = lastStatus;
}
lastStatus = s;
}
}
/// Shift content left.
public void ShiftContentLeft() {
var sortedCells = this.SelectedContentCellsBarSorted;
var firstCell = sortedCells.LastOrDefault();
if (firstCell == null) {
return;
}
MusicalElement leftElement;
var leftIndex = firstCell.BarIndex;
if (leftIndex >= 1) {
var leftBar = this.BarCells[leftIndex].Bar;
leftElement = leftBar.Elements[firstCell.LineIndex];
}
else {
leftElement = firstCell.Element;
}
leftElement.Status = firstCell.Status;
LineStatus lastStatus = null;
var reversedCells = sortedCells.Reverse();
foreach (var cell in reversedCells) {
var s = cell.Element.Status;
if (lastStatus != null) {
cell.Element.Status = lastStatus;
}
lastStatus = s;
}
}
///
/// Shifts the line octaves.
///
/// The number of octaves.
public void ShiftOctavesInSelection(int numberOfOctaves) { //// int lineIndex,
var elements = this.SelectedElements;
if (elements == null) {
return;
}
foreach (var element in elements.Where(element => element != null)) {
element.ShiftOctave(numberOfOctaves);
}
}
#region Public methods - Make cells
///
/// Makes the content of the cells from.
///
/// Content of the given.
public void MakeCellsFromContent(IMusicalContent givenContent, EditorContent givenContentType, bool givenIsMusic) {
this.ResetCells();
this.cornerCell = new CornerCell(this) {
PenBrush = Brushes.Black,
ContentBrush = Brushes.WhiteSmoke,
LineIndex = -1,
BarIndex = -1,
};
this.MakeBarCells(givenContent.ContentBars);
this.MakeLineCells(givenContent.ContentLines);
this.MakeContentCells(givenContent.ContentLines, givenContent.ContentElements);
//// this.LoadVoices(givenContent);
this.PrepareGroupCells(givenContentType, givenIsMusic);
this.RebuildAllCells();
}
///
/// Rebuilds all cells.
///
public void RebuildAllCells() {
//// Put all cells to one list.
this.AllCells = new List();
this.AllCells.Add(this.cornerCell);
this.AllCells.AddRange(this.BarCells);
this.AllCells.AddRange(this.LineCells);
foreach (var line in this.EditorLines) {
this.AllCells.AddRange(line.GroupCells);
}
/*
foreach (var line in this.EditorLines) {
this.AllCells.AddRange(line.EditorCells);
} */
}
/// Prepares the cells.
public void PrepareGroupCells(EditorContent givenContentType, bool givenIsMusic) {
this.ContentType = givenContentType;
foreach (var editorLine in this.EditorLines) {
GroupCell lastGroupCell = null;
editorLine.GroupCells = new List();
foreach (var cell in editorLine.ContentCells) {
cell.Element.PrepareContent(this.ContentType);
if (lastGroupCell == null || lastGroupCell.FirstElement == null) {
lastGroupCell = new GroupCell(this, cell);
continue;
}
if (givenIsMusic && cell.Element != null && lastGroupCell.FirstElement.IsCompatibleWith(cell.Element)) {
lastGroupCell.AddInnerCell(cell);
}
else {
editorLine.GroupCells.Add(lastGroupCell);
lastGroupCell = new GroupCell(this, cell);
}
}
if (lastGroupCell != null) {
editorLine.GroupCells.Add(lastGroupCell);
}
}
this.RebuildAllCells();
}
///
/// Composes from plan.
///
public void ComposeFromPlan() {
foreach (var editorLine in this.EditorLines) {
foreach (var cell in editorLine.ContentCells) {
cell.Element.ComposeFromPlan();
}
}
}
#endregion
#region Public methods - Find cell
///
/// Gets the cell.
///
/// Index of the line.
/// Index of the cell.
/// Returns value.
public ContentCell GetContentCell(int lineIndex, int cellIndex) {
ContentCell cell = null;
if (lineIndex >= 0 && lineIndex < this.EditorLines.Count) {
var editorLine = this.EditorLines[lineIndex];
if (cellIndex >= 0 && cellIndex < editorLine.ContentCells.Count) {
cell = editorLine.ContentCells[cellIndex];
}
}
return cell;
}
/// Gets voice cell.
/// The point.
/// The voice cell.
public VoiceCell GetVoiceCell(Point point) {
var cell = (from c in this.VoiceCells
where c.ContainsPoint(point)
select c).FirstOrDefault();
return cell;
}
/// Gets the cell.
/// The point.
/// Returns value.
public BaseCell GetCellAtMousePoint(Point point) {
var cell = (from c in this.AllCells
where c.ContainsPoint(point)
select c).FirstOrDefault();
//// Check: if (cell != null) { cell.ContentBrush = Brushes.Yellow; }
return cell;
}
///
/// Gets the editor track.
///
/// The track identifier.
/// Returns value.
public EditorLine GetEditorLine(Guid lineIdent) {
var editorLine = (from t in this.EditorLines
where t.Line.LineIdent == lineIdent
select t).FirstOrDefault();
return editorLine;
}
#endregion
#region Public methods - Change selection
///
/// Clears the selection.
///
public void ClearSelection() {
this.IsSelectionInProgress = false;
foreach (var cell in this.ContentCells) {
if (cell.IsSelected) {
cell.IsSelected = false;
}
}
}
///
/// Selects the line.
///
/// The track identifier.
/// if set to true [selected].
/// if set to true [inversion].
public void SelectCellsOfLine(Guid lineIdent, bool selected, bool inversion) {
var editorLine = this.GetEditorLine(lineIdent);
foreach (var cell in editorLine.ContentCells) {
var flag = true;
if (inversion) {
flag = !cell.IsSelected;
}
else {
flag = selected;
}
cell.IsSelected = flag;
}
}
///
/// Selects the next.
///
/// Motion up.
/// Motion right.
public void SelectNext(int up, int right) { //// bool select
var c = this.SelectedCell;
if (c == null) {
return;
}
var p = c.Point;
//// if (p.BarNumber == 0) { return; }
var nextLineIndex = p.LineIndex + up;
var nextCellIndex = c.CellIndex + right;
BaseCell cell = null;
if (nextLineIndex >= 0 && nextLineIndex < this.LineCells.Count) {
if (nextCellIndex >= 0) {
cell = this.GetContentCell(nextLineIndex, nextCellIndex);
}
else {
cell = this.LineCells[nextLineIndex];
}
}
else {
if (nextCellIndex >= 0 && nextCellIndex < this.BarCells.Count) {
cell = this.BarCells[nextCellIndex];
}
}
if (cell != null) {
this.SelectedCellChanged(cell);
//// this.SetHighlightedCell(cell);
}
}
///
/// Selected cell changed.
///
/// The given cell.
public void SelectedCellChanged(BaseCell givenCell) {
if (givenCell == null) {
return;
}
this.SelectedCell = givenCell;
if (givenCell.GetType() == typeof(BarCell)) {
EditorInspector.Singleton.EnablePanel(InspectorType.Bar);
BarCell barCell = givenCell as BarCell;
if (barCell != null) {
var mbar = barCell.Bar;
EditorEventSender.Singleton.SendEditorChangedEvent(mbar);
EditorInspector.Singleton.InspectBar.ResetIdentifiers();
EditorInspector.Singleton.InspectBar.AddMainIdentifiers(mbar.Identifiers);
}
}
if (givenCell.GetType() == typeof(LineCell)) {
EditorInspector.Singleton.EnablePanel(InspectorType.Track);
LineCell lineCell = givenCell as LineCell;
if (lineCell?.Line != null) {
EditorEventSender.Singleton.SendEditorChangedEvent(lineCell.Line);
EditorInspector.Singleton.InspectLine.ResetIdentifiers();
EditorInspector.Singleton.InspectLine.AddMainIdentifiers(lineCell.Line.Identifiers);
}
}
if (givenCell.GetType() == typeof(ContentCell)) {
EditorInspector.Singleton.EnablePanel(InspectorType.Element);
ContentCell contentCell = givenCell as ContentCell;
if (contentCell?.Element != null) {
EditorEventSender.Singleton.SendEditorChangedEvent(contentCell.Element);
EditorInspector.Singleton.InspectElement.ResetIdentifiers();
EditorInspector.Singleton.InspectElement.AddMainIdentifiers(contentCell.Identifiers);
}
}
if (givenCell.GetType() == typeof(GroupCell)) {
EditorInspector.Singleton.EnablePanel(InspectorType.Element);
GroupCell groupCell = givenCell as GroupCell;
if (groupCell?.FirstElement != null) {
EditorEventSender.Singleton.SendEditorChangedEvent(groupCell.FirstElement);
EditorInspector.Singleton.InspectElement.ResetIdentifiers();
EditorInspector.Singleton.InspectElement.AddMainIdentifiers(groupCell.FirstElement.Identifiers);
}
}
}
#endregion
#region Public methods - Change cells
///
/// Sets the purpose to all elements.
///
/// The given purpose.
public void SetPurposeToEditorCells(LinePurpose givenPurpose) {
foreach (var line in this.EditorLines) {
foreach (var cell in line.ContentCells) {
if (cell?.Status != null) {
//// if ((givenPurpose == LinePurpose.Fixed) && !cell.Status.HasContent) { //// || cell?.Status.IsEmpty
//// continue; }
cell.Status.LocalPurpose = givenPurpose;
}
}
}
}
///
/// Sets the purpose to editor line cells.
///
/// The line.
/// The given purpose.
public void SetPurposeToEditorLineCells(EditorLine line, LinePurpose givenPurpose) {
foreach (var cell in line.ContentCells) {
if (cell?.Status != null) {
//// if ((givenPurpose == LinePurpose.Fixed) && !cell.Status.HasContent) { //// || cell?.Status.IsEmpty
//// continue; }
cell.Status.LocalPurpose = givenPurpose;
}
}
}
#endregion
#region Private methods - Make Cells
///
/// Resets the cells.
///
private void ResetCells() {
this.BarCells = new List();
this.LineCells = new List();
//// this.EditorCells = new List();
this.AllCells = new List();
this.VoiceCells = new List();
this.EditorLines = new List();
}
/// Loads the bars.
/// The musical plan.
private void MakeBarCells(List musicalBars) {
this.NumberOfBars = musicalBars.Count;
foreach (var bar in musicalBars) {
//// var cell = this.BarCells[bar.BarIndex];
var cell = new BarCell(this, bar) {
PenBrush = Brushes.Black,
ContentBrush = Brushes.WhiteSmoke,
Width = SeedSize.CurrentWidth - SeedSize.BasicMargin,
Height = (2 * SeedSize.CurrentHeight) - SeedSize.BasicMargin
};
if (bar.HarmonicStructure != null) {
cell.HarmonicStructure = bar.HarmonicStructure;
}
this.BarCells.Add(cell);
}
}
///
/// Loads the lines.
///
/// The content lines.
private void MakeLineCells(List contentLines) {
//// this.Lines = contentLines;
this.NumberOfLines = contentLines.Count;
foreach (var line in contentLines) {
//// var cell = this.LineCells[line.LineIndex];
var cell = new LineCell(this, line) {
PenBrush = Brushes.Black,
ContentBrush = Brushes.WhiteSmoke,
Width = (2 * SeedSize.CurrentWidth) - SeedSize.BasicMargin,
Height = SeedSize.CurrentHeight - SeedSize.BasicMargin
};
this.LineCells.Add(cell);
}
}
///
/// Loads the elements.
///
/// The content lines.
/// The given elements.
private void MakeContentCells(List contentLines, IList givenElements) {
this.EditorLines = new List();
foreach (var line in contentLines) {
var editorLine = new EditorLine(line);
var lineElements = (from element in givenElements
where element.Line.LineIdent == line.LineIdent
select element).ToList();
//// int cellIndex = 0;
foreach (var element in lineElements) {
var cell = new ContentCell(this, element);
editorLine.ContentCells.Add(cell);
//// cellIndex++;
}
foreach (var cell in editorLine.ContentCells) {
cell.PenBrush = Brushes.DarkGray;
cell.ContentBrush = Brushes.GhostWhite;
cell.Width = SeedSize.CurrentWidth - SeedSize.BasicMargin;
cell.Height = SeedSize.CurrentHeight - SeedSize.BasicMargin;
}
this.EditorLines.Add(editorLine);
}
}
#endregion
}
}